﻿using System;
using System.Collections.Generic;
using System.Data;
using Loki.Data;
using Luna.Model.Common;

namespace Luna.Data.Common
{
    /// <summary>
    /// Mapper for data interface ISearchData.
    /// </summary>
    public partial class SearchDataMapper : LunaMapper<SearchData, ISearchData> 
    {
        public SearchDataMapper()
        {
            GetAllRequest = DefaultGetAllRequest;
            GetRequest = DefaultGetRequest;
            InsertRequest = DefaultInsertRequest;
            UpdateRequest = DefaultUpdateRequest;
            DeleteRequest = DefaultDeleteRequest;
            GetAllBySearchFieldRequest = DefaultGetAllBySearchFieldRequest;
            
            Initialize();
        }
        
        partial void Initialize();
        
        private const string DefaultGetAllRequest = "Meta.search_select_all";
        private const string DefaultGetRequest = "Meta.search_select";
        private const string DefaultInsertRequest = "Meta.search_insert";
        private const string DefaultUpdateRequest = "Meta.search_update";
        private const string DefaultDeleteRequest = "Meta.search_delete";
        private const string DefaultGetAllBySearchFieldRequest = "Meta.search_select_by_searchfield";
        
        private const string DisplayNameColumnName = "DisplayName";
        private const string ItemTypeColumnName = "ItemType";
        private const string SearchFieldColumnName = "SearchField";
        public string GetAllBySearchFieldRequest {get; set; }

        protected override void LoadCoreData(ISearchData entity, IDataRecord record)
        {
            entity.DisplayName = record.GetString(DisplayNameColumnName);
            entity.ItemType = record.GetEnum<SearchItemType>(ItemTypeColumnName);
            entity.SearchField = record.GetString(SearchFieldColumnName);
        }
        
        protected override void MapSaveParameters(IDatabaseCommand command, ISearchData entity)
        {
            command.AddParameter(DisplayNameColumnName, entity.DisplayName);
            command.AddParameter(ItemTypeColumnName, entity.ItemType);
            command.AddParameter(SearchFieldColumnName, entity.SearchField);
            command.AddParameter(IDParameterName, entity.PK);
        }
        
        public IEnumerable<ISearchData> GetAllBySearchField(IDbTransaction transaction, string searchField)
        {
            return ExecuteGetAll(
                    transaction,
                    GetAllBySearchFieldRequest,
                    c =>
                    {
                        c.AddParameter(SearchFieldColumnName, searchField); 
                    });
        }
    }
}